﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cspl.AnimalFight.Lib.Interface;
using Cspl.AnimalFight.Lib.EventHandlers;

namespace Cspl.AnimalFight.Lib.Data
{
    public class Dog : AnimalBase, IAttack, IDefense, IAttackable
    {
        private DogMood _mood;

        public event DefenseDelegate defenseEvent;
        public event AttackDelegate AttackEvent;

        public bool IsDefending
        {
            get;
            set;
        }

        public void Defense()
        {
            if (defenseEvent != null)
            {
                defenseEvent(this, new DefenseEventHandlers 
                {   AnimalName = this.AnimalName,
                    Target = Animals.Dog });

                Console.WriteLine(this._animalName + " is now defend.");
            }
        }

        public void Attack(string targetName)
        {
            int theRealAttackPower = 0;

            if (this.Mood == DogMood.Bad)
            {
                theRealAttackPower = (int)(0.5 * this.AP);
            }
            else
            {
                theRealAttackPower = this.AP;
            }

            if (AttackEvent != null)
            {
                // cannot attack if already dead, bug fixed by Havit
                if (this.IsDead)
                {
                    Console.WriteLine("{0} is dead, cannot attack.", this.AnimalName);
                }
                // cannot attack if already escaped, bug fixed by Mario
                else if (this.HasEscaped)
                {
                    Console.WriteLine("{0} has escaped, cannot attack.", this.AnimalName);
                }
                // cannot attack itself bug fixed by Agni
                else if (this._animalName == targetName)
                {
                    Console.WriteLine("Cannot attack itself.");
                }
                else
                {
                    AttackEventHandlers eventhandler = new AttackEventHandlers
                    {
                        AnimalTargetName = targetName,
                        AP = theRealAttackPower
                    };

                    AttackEvent(this, eventhandler);
                }
            }
            
        }

        

        public DogMood Mood
        {
            get { return _mood; }
            set { _mood = value; }
        }


        public void Attacked(int ap)
        {
            if (this.HasEscaped)
            {
                Console.WriteLine("{0} has Escaped, Cannot be Attacked.", this.AnimalName);
            }
            else if (this.IsDead)
            {
                Console.WriteLine("{0} is alreasy dead. Cannot be attacked.", this.AnimalName);
            }
            else
            {
                Console.WriteLine("{0} is being attacked. Current HP ={1}.", this.AnimalName, this.HP);

                int realDP = this.IsDefending ? this.DP + 10 : this.DP;
                realDP = this.Mood == DogMood.Bad ? realDP - 10 : realDP;
                // if the attack is less powerfull than defense then it will be absorbed 
                this.HP -= (ap - realDP);

                Console.WriteLine("{0} has been attacked. -{1} ", this.AnimalName, ap - realDP);
                Console.WriteLine("Current HP for {0} = {1}", this.AnimalName, this.HP);

                if (this.HP <= 0)
                {
                    Console.WriteLine("the {0} is dead", this.AnimalName);
                    this.IsDead = true;
                }
            }
        }
    }

    public enum DogMood { Good, Bad }
}
